package com.fxkj.cloud.drms.infra.mq.mqtt.sender;

import com.fxkj.cloud.dbs.common.model.ControlOrderCommand;
import com.fxkj.cloud.dbs.common.model.SimpleControlOrder;
import com.fxkj.cloud.dbs.common.model.attribute.AttributeConstants;
import com.fxkj.cloud.dbs.common.model.linkage.KvDTO;
import com.fxkj.cloud.drms.domain.mqtt.CommandDomain;
import com.fxkj.cloud.drms.domain.mqtt.DeviceControlOrderCommand;
import com.fxkj.common.utils.JacksonUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
 * <p>description: mqtt命令 </p>
 * @author ww
 * @date 2025/3/25 10:42
 */
public class MqttCommand extends BaseMqttMessage<MqttCommandKv> implements Serializable {

    public MqttCommand(UUID requestId, List<MqttCommandKv> commandKvs) {
        super(requestId, commandKvs);
    }

    public static MqttCommand create(CommandDomain command) {
        UUID requestId = command.getCommandId();
        List<MqttCommandKv> commandKvs = command.getCommandKvs().stream().map(MqttCommandKv::create).toList();
        return new MqttCommand(requestId, commandKvs);
    }

    public static MqttCommand create(List<KvDTO> kvs) {
        UUID requestId = UUID.randomUUID();
        List<MqttCommandKv> commandKvs = kvs.stream().map(MqttCommandKv::create).toList();
        return new MqttCommand(requestId, commandKvs);
    }

    public static MqttCommand createControlPlan(DeviceControlOrderCommand deviceControlOrderCommand) {
        return new MqttCommand(
                deviceControlOrderCommand.getRequestId(),
                Collections.singletonList(
                        new MqttCommandKv(
                                AttributeConstants.Service.C.Code.P.getFullCode(),
                                ControlOrderCommand.create(
                                        deviceControlOrderCommand.getLcNos(),
                                        deviceControlOrderCommand.getControlOrders()
                                                .stream()
                                                .map(SimpleControlOrder::create)
                                                .toList()
                                )))
        );
    }

    public String toJsonString() {
        return JacksonUtil.writeValueAsString(this);
    }

//    @Override
//    protected Integer getType() {
//        return MqttMessageType.COMMAND.getCode();
//    }
}
